package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionHandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-10-30
 */
@RestController
@RequestMapping("/eduservice/edu-video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;

    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
     videoService.save(eduVideo);
     return R.ok();
    }

    @DeleteMapping("{id}")
      public  R deleteVideo(@PathVariable String id){
        EduVideo eduVideo = videoService.getById(id);
         String videoSourceId = eduVideo.getVideoSourceId();
         if(!StringUtils.isEmpty(videoSourceId)){
             R r = vodClient.removeAlyVideo(videoSourceId);
             if(r.getCode()==20001){
                 throw new GuliException(20001,"删除视频失败，熔断器");
             }
         }

        videoService.removeById(id);
        return R.ok();
     }


}

